package com.meichis.mcsappframework.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.meichis.mcsappframework.R;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.mcsappframework.widget.SwitchButton;

import java.text.DecimalFormat;

/**
 * Created by chenhaifei on 2016/8/17.
 */
public class NumberPickerDialog extends Dialog implements OnClickListener {
    Button btn_ok;
    Button btn_cancel;
    TextView txt_input;
    TextView txt_rangeMin;
    TextView txt_rangeMax;
    TextView txt_inputunit;
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    Button btn_6;
    Button btn_7;
    Button btn_8;
    Button btn_9;
    Button btn_0;
    Button btn_clear;
    Button btn_dot;
    TextView txtTitle;
    SwitchButton sb_PriceType;

    Context context;
    String initNumber;
    int max = -1000, unit = 1;

    Boolean IsDecimal = false;
    double decimalmin = -1000, decimalmax = 99999;

    String title = "数字录入";
    String PackT = "";
    String PackP = "";
    int TypeNum = 1;
    boolean checkstate = true;
    private Double datanum;

    private double mediant = 0;

    public void setDatanum(double datanum) {
        this.datanum = datanum;
    }

    public void setMax(int value) {
        max = value;
    }

    public boolean getCheckstate() {
        return checkstate;
    }

    public void setunit(int value) {
        unit = value;
        if (unit > 1) {
            sb_PriceType.setVisibility(View.VISIBLE);
        }
    }

    public String GetInitnumber() {
        return initNumber;
    }

    public void setType(int Type) {
        TypeNum = Type;
        if (Type == 2) {
            sb_PriceType.setVisibility(View.VISIBLE);
        }
        sb_PriceType.setText(null);
        txt_input.setText("");

    }

    public void setPack(String packT, String packP) {
        this.PackT = packT;
        PackP = packP;
    }

    public void setDecimalMinMAX(double min, double max) {
        decimalmin = min;
        decimalmax = max;
    }

    public void SetIsDecimal(Boolean flag) {
        IsDecimal = flag;
        if (!IsDecimal && btn_dot != null) {
            btn_dot.setText("<-");
        }
    }

    /**
     * 单独设置标题背景
     *
     * @param resid
     */
    public void setTitleBackground(int resid) {
        txtTitle.setBackgroundResource(resid);
    }

    /**
     * 单独设置标题背景
     *
     * @param color
     */
    public void setTitleBackgroundColor(int color) {
        txtTitle.setBackgroundColor(color);
    }

    public void setUnit(String unit) {
        if (txt_inputunit != null) {
            txt_inputunit.setText(unit);
        }
    }

    public interface OnMyNumberSetListener {
        /**
         * 数字被设定之后执行此方法
         *
         * @param number 当前文字框中字符串
         */
        void onNumberSet(String number);
    }

    private OnMyNumberSetListener mListener;

    public NumberPickerDialog(Context context, String number, OnMyNumberSetListener listener) {
        super(context);
        this.context = context;
        this.mListener = listener;
        this.initNumber = number;
    }

    public NumberPickerDialog(Context context, String number, String _title, OnMyNumberSetListener listener) {
        super(context);
        this.context = context;
        this.mListener = listener;
        this.initNumber = number;
        try {
            this.mediant = Double.parseDouble(number);
        } catch (Exception e) {
            this.mediant = 0;
        }

        this.title = _title;

    }

    /**
     * 单独设置按钮背景
     *
     * @param resid
     */
    public void setButtonBackground(int resid) {
        btn_ok.setBackgroundResource(resid);
        btn_cancel.setBackgroundResource(resid);
    }

    public void setNumberButtonBackground(int resid) {
        btn_1.setBackgroundResource(resid);
        btn_2.setBackgroundResource(resid);
        btn_3.setBackgroundResource(resid);
        btn_4.setBackgroundResource(resid);
        btn_5.setBackgroundResource(resid);
        btn_6.setBackgroundResource(resid);
        btn_7.setBackgroundResource(resid);
        btn_8.setBackgroundResource(resid);
        btn_9.setBackgroundResource(resid);
        btn_0.setBackgroundResource(resid);
        btn_clear.setBackgroundResource(resid);
        btn_dot.setBackgroundResource(resid);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_number_picker);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText(title);

        sb_PriceType = (SwitchButton) findViewById(R.id.sb_PriceType);

        txt_input = (TextView) findViewById(R.id.txt_inputNumber);
        txt_inputunit = (TextView) findViewById(R.id.txt_inputunit);
        txt_input.setText(String.valueOf(initNumber));

        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_0 = (Button) findViewById(R.id.btn_0);

        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_dot = (Button) findViewById(R.id.btn_dot);
        btn_ok = (Button) findViewById(R.id.ok);
        btn_cancel = (Button) findViewById(R.id.cancel);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_0.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_dot.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        sb_PriceType.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (TypeNum == 1) {
                    txt_inputunit.setText("元/" + (b ? PackT : PackP));
                    String newNumber = mediant + "";
                    if (mediant == 0) {
                        newNumber = txt_input.getText().toString();
                    }
                    if (TextUtils.isEmpty(newNumber)) {
                        return;
                    }
                    if (b) {
                        txt_input.setText(Tools.getFormatString(Math.round(Double.parseDouble(newNumber) * unit * 100) / 100.0));
                        mediant = Math.round(Double.parseDouble(newNumber) * unit * 10000) / 10000.0;
                    } else {
                        txt_input.setText(Tools.getFormatString(Math.round(Double.parseDouble(newNumber) / unit * 100) / 100.0));
                        mediant = Math.round(Double.parseDouble(newNumber) / unit * 10000) / 10000.0;
                    }
                } else if (TypeNum == 2) {
                    if (b) {
                        txt_input.setText("");
                        txt_inputunit.setText("%");
                        txtTitle.setText("优惠\n" + "请输入折扣率(0~100)");
                        checkstate = true;
                        decimalmax = 100;
                        decimalmin = 0;
                    } else {
                        txt_input.setText("");
                        txt_inputunit.setText("元");
                        txtTitle.setText("优惠\n" + "请输入一个0~" + datanum + "之间的数字");
                        checkstate = false;
                        decimalmin = 0;
                        decimalmax = datanum;
                    }
                }
            }
        });
        setCancelable(false);
        if (!IsDecimal && btn_dot != null) {
            btn_dot.setText("<-");
        }
    }

    private void setText(String num) {
        String nowNumber = txt_input.getText().toString();
        String newNumber = "";

		/* 限制最多位数为8 */
        if (nowNumber.length() >= 8) {
            return;
        }

		/* 限制为两位小数 */
        int dotSite = nowNumber.indexOf(".");
        if (dotSite > 0 && dotSite + 2 < nowNumber.length()) {
            return;
        }

        if (!num.equals(".")) {// 输入不为小数点
            if (nowNumber.equals("") || nowNumber.equals("0")) {
                newNumber = String.valueOf(num);
            } else {
                newNumber = nowNumber.concat(String.valueOf(num));
            }
        } else {// 输入小数点
            if (nowNumber.equals("") || nowNumber.contains(".")) {
                // 当前值为空或者已包含小数点时不执行操作
                return;
            } else {
                newNumber = nowNumber.concat(".");
            }
        }
        if (max != -1000 && Integer.parseInt(newNumber) > max) {
            newNumber = max + "";
        }
        if (IsDecimal) {
            double value = 0;
            if (TypeNum == 1) {
                if (sb_PriceType.isChecked()) {
                    value = Math.round(Double.parseDouble(newNumber) / unit * 10000) / 10000.0;
                } else {
                    value = Math.round(Double.parseDouble(newNumber) * 10000) / 10000.0;
                }
            } else {
                value = Math.round(Double.parseDouble(newNumber) * 10000) / 10000.0;
            }
            if (Double.compare(value, decimalmax) > 0) {
                if (TypeNum == 1) {
                    if (sb_PriceType.isChecked()) {
                        newNumber = initNumber;
                        double MAX = Math.round(decimalmax * unit * 100) / 100.0;
                        double MIN = Math.round(decimalmin * unit * 100) / 100.0;
                        MessageTools.showLongToast("请输入" + MIN + "至" + MAX + "区间的值");
                    } else {
                        newNumber = Math.round(Double.parseDouble(initNumber) / unit * 10000) / 10000.0 + "";
                        MessageTools.showLongToast( "请输入" + Math.round(decimalmin * 100) / 100.0 + "至" + Math.round(decimalmax * 100) / 100.0 + "区间的值");
                    }
                } else {
                    MessageTools.showLongToast( "请输入" + Math.round(decimalmin * 100) / 100.0 + "至" + Math.round(decimalmax * 100) / 100.0 + "区间的值");
                    txt_input.setText("");
                    return;
                }
            }

        }
        mediant = 0;
        txt_input.setText(newNumber);
    }

    private void deleteText() {
        txt_input.setText("0");
        mediant=0;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ok) {
            String number = txt_input.getText().toString();
            if (mediant != 0) {
                number =new DecimalFormat("0.####").format(mediant);
            }
            if (TextUtils.isEmpty(number)) {
                number = "0";
            }
            if (number.endsWith(".")) {
                number = number.substring(0, number.length() - 1);
            }
            if (max != -1000 && Integer.parseInt(number) > max) {
                number = max + "";
            }
            if (IsDecimal) {
                double value = 0;

                if (TypeNum == 1) {
                    if (sb_PriceType.isChecked()) {
                        value = Math.round(Double.parseDouble(number) / unit * 10000) / 10000.0;
                    } else {
                        value = Math.round(Double.parseDouble(number) * 10000) / 10000.0;
                    }
                } else {
                    value = Math.round(Double.parseDouble(number) * 10000) / 10000.0;
                }

                if (Double.compare(value, decimalmin) < 0 || Double.compare(value, decimalmax) > 0) {
                    if (TypeNum == 1) {
                        if (sb_PriceType.isChecked()) {
                            double MAX = Math.round(decimalmax * unit * 100) / 100.0;
                            double MIN = Math.round(decimalmin * unit * 100) / 100.0;
                            MessageTools.showLongToast("请输入" + MIN + "至" + MAX + "区间的值");
                        } else {
                            MessageTools.showLongToast("请输入" + Math.round(decimalmin * 100) / 100.0 + "至" + Math.round(decimalmax * 100) / 100.0 + "区间的值");
                        }
                        return;
                    } else {
                        MessageTools.showLongToast( "请输入" + Math.round(decimalmin * 100) / 100.0 + "至" + Math.round(decimalmax * 100) / 100.0 + "区间的值");
                    }
                    return;
                }
                mListener.onNumberSet(value + "");
            } else {
                mListener.onNumberSet(number);
            }

            dismiss();
        }
        if (v.getId() == R.id.cancel) {
            dismiss();
        }
        if (v.getId() == R.id.btn_0) {
            setText("0");
        }
        if (v.getId() == R.id.btn_1) {
            setText("1");
        }
        if (v.getId() == R.id.btn_2) {
            setText("2");
        }
        if (v.getId() == R.id.btn_3) {
            setText("3");
        }
        if (v.getId() == R.id.btn_4) {
            setText("4");
        }
        if (v.getId() == R.id.btn_5) {
            setText("5");
        }
        if (v.getId() == R.id.btn_6) {
            setText("6");
        }
        if (v.getId() == R.id.btn_7) {
            setText("7");
        }
        if (v.getId() == R.id.btn_8) {
            setText("8");
        }
        if (v.getId() == R.id.btn_9) {
            setText("9");
        }
        if (v.getId() == R.id.btn_dot) {
            if (!IsDecimal) {
                String num = txt_input.getText().toString();
                if (num.length() == 1) {
                    deleteText();
                }
                if (num.length() > 1) {
                    txt_input.setText(num.substring(0, num.length() - 1));
                }
            } else {
                setText(".");
            }
        }
        if (v.getId() == R.id.btn_clear) {
            deleteText();
        }

    }
}


